﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class WorksheetRepository : RepositoryBase<Worksheet>
    {

        //public int GetLastWorksheetCount(string number, int workType = 0)
        public int GetLastWorksheetCount(string number)
        {
            var result = from ws in dbcontext.Worksheets
                         join pro in dbcontext.Products 
                         on ws.ParentId equals pro.Id
                         //where ws.Number.Contains(number) && pro.WorkType== workType
                         where ws.Number.Contains(number)
                         select new ProductTechnologyView
                         {
                             //WorkType = pro.WorkType
                         };

            return result.ToList().Count;
        }

        public IQueryable<ProductTechnologyView> GetList(int status, string productName, string productNum, DateTime beginTime, DateTime endTime)
        {
            var result = from ws in dbcontext.Worksheets
                         join pro in dbcontext.Products
                         on ws.ProductId equals pro.Id
                         join ord in dbcontext.Orders
                         on ws.OtherId equals ord.Id into ordEmp
                         from ord in ordEmp.DefaultIfEmpty()
                         where ((status == 4 && ws.Status != -1 && ws.Status != -101 && ws.ExamineStatus != 1) || (status == 5 && ws.ExamineStatus == 1) || (status == -1 && (ws.Status == -1 || ws.Status == -101)) || ws.Status == status)
                         && ord.Stage != -10
                         && (productName == "" || pro.Name.Contains(productName))
                               && (productNum == "" || pro.Number.Contains(productNum))
                               && beginTime <= ws.ApplyTime
                               && endTime >= ws.ApplyTime
                         select new ProductTechnologyView
                         {
                             ProductId = pro.Id,
                             ProductName = pro.Name,
                             ProductNumber = pro.Number,
                             Status = ws.Status,
                             CreatorName = ord != null ? ord.CreatorName : ws.CreatorName,
                             OrderNum = ord != null ? ord.Number : "",
                             ApplyTime = ws.ApplyTime,
                             WorksheetId = ws.Id,
                             Amount = ws.WorkAmount,
                             StandardAmount = ws.StandardAmount,
                             ParentId = ws.ParentId,
                             //Type = ws.Type,
                             SendTime = ord.SendTime,
                             //WorkType = pro.WorkType
                         };
            return result;
        }

        public IQueryable<ProductTechnologyView> GetListWhere(int status, string productName, string productNum, DateTime beginTime, DateTime endTime)
        {
            var result = from ws in dbcontext.Worksheets
                         join pro in dbcontext.Products
                         on ws.ProductId equals pro.Id
                         join ord in dbcontext.Orders
                         on ws.OtherId equals ord.Id into ordEmp
                         from ord in ordEmp.DefaultIfEmpty()
                         where ((status == 4 && ws.Status != -1 && ws.Status != -101 && ws.ExamineStatus != 1) || (status == 5 && ws.ExamineStatus == 1) || (status == -1 && (ws.Status == -1 || ws.Status == -101)) || ws.Status == status)
                         && ws.StandardAmount < ws.WorkAmount
                         && ord.Stage != -10
                         && (productName == "" || pro.Name.Contains(productName))
                               && (productNum == "" || pro.Number.Contains(productNum))
                               && beginTime <= ws.ApplyTime
                               && endTime >= ws.ApplyTime
                         select new ProductTechnologyView
                         {
                             ProductId = pro.Id,
                             ProductName = pro.Name,
                             ProductNumber = pro.Number,
                             Status = ws.Status,
                             CreatorName = ord != null ? ord.CreatorName : ws.CreatorName,
                             OrderNum = ord != null ? ord.Number : "",
                             ApplyTime = ws.ApplyTime,
                             WorksheetId = ws.Id,
                             Amount = ws.WorkAmount,
                             StandardAmount = ws.StandardAmount,
                             ParentId = ws.ParentId,
                             //Type = ws.Type,
                             SendTime = ord.SendTime,
                             //WorkType = pro.WorkType,
                             WorksheetStatus = 1
                         };

            var result1 = from ws in dbcontext.Worksheets
                          join pro in dbcontext.Products
                          on ws.ProductId equals pro.Id
                          join ord in dbcontext.Orders
                          on ws.OtherId equals ord.Id into ordEmp
                          from ord in ordEmp.DefaultIfEmpty()
                          where ((status == 4 && ws.Status != -1 && ws.Status != -101 && ws.ExamineStatus != 1) || (status == 5 && ws.ExamineStatus == 1) || (status == -1 && (ws.Status == -1 || ws.Status == -101)) || ws.Status == status)
                          && ws.StandardAmount >= ws.WorkAmount
                          && ord.Stage != -10
                          && (productName == "" || pro.Name.Contains(productName))
                                && (productNum == "" || pro.Number.Contains(productNum))
                                && beginTime <= ws.ApplyTime
                                && endTime >= ws.ApplyTime
                          select new ProductTechnologyView
                          {
                              ProductId = pro.Id,
                              ProductName = pro.Name,
                              ProductNumber = pro.Number,
                              Status = ws.Status,
                              CreatorName = ord != null ? ord.CreatorName : ws.CreatorName,
                              OrderNum = ord != null ? ord.Number : "",
                              ApplyTime = ws.ApplyTime,
                              WorksheetId = ws.Id,
                              Amount = ws.WorkAmount,
                              StandardAmount = ws.StandardAmount,
                              ParentId = ws.ParentId,
                              //Type = ws.Type,
                              SendTime = ord.SendTime,
                              //WorkType = pro.WorkType,
                              WorksheetStatus = 2
                          };
            return result.Concat(result1);
        }
    }
}
